/**
 * 
 */
package kdweibo4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import kdweibo4j.http.Response;
import kdweibo4j.org.json.JSONArray;
import kdweibo4j.org.json.JSONException;
import kdweibo4j.org.json.JSONObject;


/**
 * @author hezhou
 * 
 */
public class Count implements java.io.Serializable {

	private static final long serialVersionUID = 9076424494907778181L;

	private String id;

	private long comments;

	private long rt;

	private long dm;

	private long mentions;

	private long followers;

	private long new_status;
	
	private long notices;
	
	private long statuses;
	
	private long member;
	
	private HashMap<String, Long> group_statuses = new HashMap<String, Long>(); //小组消息数
	private HashMap<String, Long> network_statuses = new HashMap<String, Long>(); //小组消息数

	// {"new_status":0,"followers":0,"notices":0,"dm":0,"statuses":0,"mentions":0,"members":136,"comments":0}
	public Count(JSONObject json) throws WeiboException, JSONException {
		id = json.getString("id");
		comments = json.getLong("comments");
		
		new_status=json.getLong("new_status");
		notices=json.getLong("notices");
		statuses=json.getLong("statuses");
		
		rt = json.getLong("rt");
		dm = json.getLong("dm");
		mentions = json.getLong("mentions");
		followers = json.getLong("followers");
	}

	Count(Response res) throws WeiboException {
		JSONObject json = res.asJSONObject();
		try {
			/*
			id = json.getString("id");
			comments = json.getLong("comments");
			rt = json.getLong("rt");
			dm = json.getLong("dm");
			mentions = json.getLong("mentions");
			followers = json.getLong("followers");
			*/
			id = json.getString("id");
			comments = json.getLong("comments");
			
			new_status=json.getLong("new_status");
			notices=json.getLong("notices");
			statuses=json.getLong("statuses");
			
			rt = json.getLong("rt");
			dm = json.getLong("dm");
			mentions = json.getLong("mentions");
			followers = json.getLong("followers");
			
			JSONObject json_group_statuses = json.getJSONObject("group_statuses");
			Iterator<String> it = json_group_statuses.keys(); 
            while(it.hasNext()){//遍历JSONObject  
                String groupID = (String) it.next().toString();  
                long newMsgCount = json_group_statuses.getLong(groupID);  
                group_statuses.put(groupID, newMsgCount);
            }  
            //社区消息数
            JSONObject json_network_statuses = json.getJSONObject("communityNotices");
			Iterator<String> itNetwork = json_network_statuses.keys(); 
            while(itNetwork.hasNext()){//遍历JSONObject  
                String networkId = (String) itNetwork.next().toString();  
                long newMsgCount = 0;
	            newMsgCount = json_network_statuses.getLong(networkId);  
                network_statuses.put(networkId, newMsgCount);
            }  
		} catch (JSONException je) {
			throw new WeiboException(je.getMessage() + ":" + json.toString(),
					je);
		}

	}

	static List<Count> constructCounts(Response res) throws WeiboException {
		try {
			System.out.println(res.asString());
			JSONArray list = res.asJSONArray();
			int size = list.length();
			List<Count> counts = new ArrayList<Count>(size);
			for (int i = 0; i < size; i++) {
				counts.add(new Count(list.getJSONObject(i)));
			}
			return counts;
		} catch (JSONException jsone) {
			throw new WeiboException(jsone);
		} catch (WeiboException te) {
			throw te;
		}
	}

	@Override
	public int hashCode() {
		return (int) id.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		return obj instanceof Count && ((Count) obj).id == this.id;
	}

	public String getId() {
		return id;
	}
	
	public long getComments() {
		return comments;
	}

	public long getRt() {
		return rt;
	}

	public long getDm() {
		return dm;
	}

	public long getMentions() {
		return mentions;
	}

	public long getFollowers() {
		return followers;
	}

	public long getStatuses() {
		return statuses;
	}

	public long getNotices() {
		return notices;
	}

	public long getMember() {
		return member;
	}

	public long getNew_status() {
		return new_status;
	}
    //小组
	public void setGroup_statuses(HashMap<String, Long> group_statuses){
		this.group_statuses = group_statuses;
	}
	
	public HashMap<String, Long> getGroup_statuses(){
		return this.group_statuses;
	}
	
	//社区
	public void setNetwork_statuses(HashMap<String, Long> network_statuses){
		this.network_statuses = network_statuses;
	}
	
	/**
	 * 与服务端约定：
	 * @return
	 */
	public HashMap<String, Long> getNetwork_statuses(){
		return this.network_statuses;
	}
	
	@Override
	public String toString() {
		return "Count{ id=" + id + ", comments=" + comments + ", rt=" + rt
				+ ", dm=" + dm + ", mentions=" + mentions + ", followers="
				+ followers + '}';
	}
}
